package com.dodayum.controller;

import com.alibaba.fastjson.JSON;
import com.dodayum.pojo.ServiceInformation;
import com.dodayum.service.ServiceInformationService;
import com.dodayum.service.WebsiteService;
import com.dodayum.utils.ResultUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

@RestController
@ResponseBody
@CrossOrigin
public class TestController {

    @Resource
    private ServiceInformationService service;

    @Resource
    private WebsiteService websiteService;

    @RequestMapping("test/testAjax")
    public void testAjax(String message, HttpServletResponse response) throws Exception {
        // 不加这个直接乱码
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        response.getWriter().println("后端收到参数" + message);
    }

    @RequestMapping(value = "test/testList")
    public String testList() {
        List<ServiceInformation> list = service.queryAll();
        return JSON.toJSONString(list);
    }

    @RequestMapping(value = "test/DB")
    public String db() {
        websiteService.test01();
        return "开始了";
    }

    /**
     * 前端表单学习用的API
     *
     * @param request 前端传递过来的东西
     */
    @RequestMapping("test/learn-web")
    public void learnWeb(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        String header = request.getHeader("Content-Type");
        String requestString;
        if ("application/json".equals(header)) {
            // JSON请求
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
            StringBuilder responseStrBuilder = new StringBuilder();
            String inputStr;
            while ((inputStr = streamReader.readLine()) != null) {
                responseStrBuilder.append(inputStr);
            }
            requestString = responseStrBuilder.toString();


        } else if ("application/x-www-form-urlencoded".equals(header)
                || "application/x-www-form-urlencoded; charset=utf-8".equals(header)) {
            // 表单请求
            requestString = JSON.toJSONString(request.getParameterMap());
        } else {
            requestString = "未知请求方式，Content-Type：" + header;
        }

        String method = request.getMethod();

        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Content-type", "text/html;charset=UTF-8");

        String resultInformation = "后端收到参数:\t" + requestString + "\n请求方式:\t" + method;
        resultInformation = new ResultUtils(true, resultInformation, 200, StringUtils.isEmpty(header) ? "null" : header)
                .buildReturn();

        response.getWriter().println(resultInformation);
    }

}
